package com.sec.android.app.myfiles.module.abstraction;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.ChangePasswordsActivity;
import com.sec.android.app.myfiles.activity.FileListActivity;
import com.sec.android.app.myfiles.activity.FolderSelectorBottomSheetActivity;
import com.sec.android.app.myfiles.dialog.ConfirmDeleteDialog;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.CopyMoveCmd;
import com.sec.android.app.myfiles.facade.cmd.DeleteRecordCmd;
import com.sec.android.app.myfiles.facade.cmd.FinishPickerCmd;
import com.sec.android.app.myfiles.facade.cmd.LockUnlockCmd.LockProgress;
import com.sec.android.app.myfiles.facade.cmd.ToSelectModeCmd;
import com.sec.android.app.myfiles.feature.KnoxMgr;
import com.sec.android.app.myfiles.feature.MouseKeyboardMgr;
import com.sec.android.app.myfiles.feature.PrivateModeMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment.ActionModeType;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.cloud.CloudFileRecord;
import com.sec.android.app.myfiles.module.local.category.CategoryFileRecord;
import com.sec.android.app.myfiles.module.optimizestorage.OptimizeStorageFileRecord;
import com.sec.android.app.myfiles.module.shortcut.ShortcutUtils;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.operation.FileOperator;
import com.sec.android.app.myfiles.operation.FileOperator.Operation;
import com.sec.android.app.myfiles.util.DrmUtils;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;
import com.sec.android.app.myfiles.util.SmartTipUtils;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.util.VZCloudUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Locale;

/**
 * <pre>
 * Implement class of Menu.
 * It define menu item for normal mode or action mode
 * and do process of menu item
 * </pre>
 *
 * @author jae.bae
 */
public abstract class AbsMenuImp implements IMenuImp {
    private static final String TAG = AbsMenuImp.class.getSimpleName();

    protected final Context mContext;
    protected NavigationInfo mNavigationInfo;
    protected AbsMyFilesFragment mFragment;
    private final StorageType mSupportStorage;

    protected int mListItemCount;
    private int mMenuResId;
    private int mActionMenuResId;
    protected FileListActivity mActivity = null;
    protected final int BACKGROUND = -1;

    protected enum PrivatePath {
        ONLY_PRIVATE,
        ONLY_NON_PRIVATE,
        BOTH,
    }

    public AbsMenuImp(Context context, StorageType supportStorage) {
        mActivity = (FileListActivity) context;
        mContext = context;
        mMenuResId = R.menu.list_menu;
        mActionMenuResId = R.menu.action_menu;
        mSupportStorage = supportStorage;
    }

    /**
     * @note Each menu of the chain have common information.
     */
    @Override
    public void setInfo(AbsMyFilesFragment fragment, NavigationInfo navigationInfo) {
        mFragment = fragment;
        mNavigationInfo = navigationInfo;
    }

    @Override
    public void setMenuRes(int menuResId) {
        mMenuResId = menuResId;
    }

    @Override
    public void setActionMenuRes(int menuResId) {
        mActionMenuResId = menuResId;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater, ActionModeType actionMode) {
        switch (actionMode) {
            case NORMAL:
                inflateNormal(mNavigationInfo, menu, inflater);
                break;
            case SELECT_FILE_FOLDER_OPERATION:
            case EDIT_FILE_FOLDER_OPERATION:
                inflater.inflate(mActionMenuResId, menu);
                break;
            case SELECT_SHARE_FILE_OPERATION:
                inflater.inflate(R.menu.action_mode_share_done_common, menu);
                break;
            default:
                break;
        }
    }

    private void inflateNormal(NavigationInfo navigationInfo, Menu menu, MenuInflater inflater) {
        if (navigationInfo != null) {
            switch (navigationInfo.getNavigationMode()) {
                case Normal:
                    inflater.inflate(mMenuResId, menu);
                    break;
                case Pick_files:
                    inflater.inflate(R.menu.action_mode_pick_multiple_files, menu);
                    break;
                case Preview_compress_item:
                    inflater.inflate(R.menu.action_mode_menu_preview_compress, menu);
                    break;
                case Optimize_storage_files:
                    inflater.inflate(R.menu.action_mode_menu_optimize_storage, menu);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public final void onPrepareOptionsMenu(Menu menu, ActionModeType actionMode) {
        if (mFragment != null && !mFragment.isSelectActionMode()) {
            mActivity.setSearchMenu(menu);
            mListItemCount = mFragment.getListItemCount();
            onPrepareNormalOptionsMenu(menu);
        }
    }

    @Override
    public final void onPrepareActionMenu(Menu menu, ActionModeType actionMode) {
        onPrepareEditOptionsMenu(menu);
    }

    @Override
    public final boolean onOptionsItemSelected(MenuItem item) {
        Log.a(this, "onOptionsItemSelected [" + mSupportStorage + "][" + item.getTitle() + "]");
        return _onOptionsItemSelected(item);
    }

    protected boolean _onOptionsItemSelected(MenuItem item) {
        boolean bRet = true;

        int id = item.getItemId();

        if (!UiUtils.isValidClick(id)) {
            return false;
        }

        int processId = mActivity.getProcessId();
        NavigationInfo info = mFragment.getNavigationInfo();
        FileRecord curRecord = null;
        if (info != null) {
            curRecord = info.getCurRecord();
        }
        switch (id) {
            case R.id.menu_share:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.SHARE, (mFragment.getSelectedFile().size() == 0) ?
                        null : SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                MyFilesFacade.shareVia(processId, mContext, mFragment, mFragment.getSelectedFile(), false);
                break;
            case R.id.menu_sort_by:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.SORT_BY, null);
                MyFilesFacade.setSortBy(processId, mFragment);
                break;
            case R.id.menu_edit:
                if (curRecord != null) {
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.EDIT, UiUtils.isCategoryFolderList(curRecord) ?
                            SamsungAnalyticsLog.SelectMode.SELECTION_CATEGORY_FOLDER_MODE : null);
                }
                MyFilesFacade.toSelectMode(processId, mNavigationInfo.getCurFragment(), ToSelectModeCmd.SelectModeType.EDIT_MODE);
                break;
            case R.id.menu_delete:
                ArrayList<FileRecord> selectedFiles = mFragment.getSelectedFile();
                if (selectedFiles != null && !selectedFiles.isEmpty()) {
                    if (curRecord != null) {
                        if (StorageType.OptimizeStorage.equals(curRecord.getStorageType())) {
                            long value = SamsungAnalyticsLog.getTotalSize(selectedFiles);
                            String detail = SamsungAnalyticsLog.getCountOfExtensions(selectedFiles);
                            SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.DELETE, value, detail, null);
                        } else {
                            SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.DELETE, UiUtils.isCategoryFolderList(curRecord) ?
                                    SamsungAnalyticsLog.SelectMode.SELECTION_CATEGORY_FOLDER_MODE : SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                        }
                    }
                    MyFilesFacade.deleteRecord(processId, mContext, mFragment, selectedFiles, DeleteRecordCmd.OperationProgress.SHOW_CONFIRM_POPUP);
                }
                break;
            case R.id.menu_remove:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.REMOVE, UiUtils.isCategoryFolderList(curRecord) ?
                        SamsungAnalyticsLog.SelectMode.SELECTION_CATEGORY_FOLDER_MODE : SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                MyFilesFacade.deleteRecord(processId, mContext, mFragment, mFragment.getSelectedFile(),
                        DeleteRecordCmd.OperationProgress.DO_OPERATE);
                break;
            case R.id.menu_copy:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.COPY, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                MyFilesFacade.copymoveRecord(processId, mContext, Operation.COPY, CopyMoveCmd.OperationProgress.SELECT_DST, mFragment,
                        mFragment.getSelectedFile(), null);
                break;
            case R.id.menu_move:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.MOVE, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                MyFilesFacade.copymoveRecord(processId, mContext, Operation.MOVE, CopyMoveCmd.OperationProgress.SELECT_DST, mFragment,
                        mFragment.getSelectedFile(), null);
                break;
            case R.id.menu_paste:
                if (AppFeatures.isKnoxDesktopMode()) {
                    if (curRecord != null) {
                        MouseKeyboardMgr.onCtrlV(mFragment, curRecord);
                    }
                }
                break;
            case R.id.menu_rename:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.RENAME, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                MyFilesFacade.renameRecord(processId, mContext, mFragment, mFragment.getSelectedFile());
                break;
            case R.id.menu_create_folder:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.CREATE_FOLDER, null);
                MyFilesFacade.createDir(processId, mContext, mFragment, mNavigationInfo.getCurRecord());
                break;
            case R.id.menu_details:
                if (curRecord != null) {
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.DETAILS, UiUtils.isCategoryFolderList(curRecord) ?
                            SamsungAnalyticsLog.SelectMode.SELECTION_CATEGORY_FOLDER_MODE : SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                }
                MyFilesFacade.showDetail(processId, mContext, mFragment, mFragment.getSelectedFile(), false);
                break;
            case R.id.menu_show_hidden_files:
            case R.id.menu_hide_hidden_files:
                MyFilesFacade.setShowHiddenFiles(processId, mContext, mNavigationInfo.getCurFragment());
                UiUtils.sendLocalBroadcastIntent(mContext, AppConstants.MYFILES_ACTION_SHOW_HIDDEN_FILES_CHANGED, processId);
                break;
            case R.id.menu_add_shortcut:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.ADD_SHORTCUT, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                MyFilesFacade.addShortcut(mActivity.getProcessId(), mContext, mFragment, mFragment.getSelectedFile());
                break;
            case R.id.menu_pick_done:
                if (NavigationManager.getInstance(processId).isFromPrivateBox())
                    MyFilesFacade.importFromApp(processId, mFragment);
                else {
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.DONE, null);
                    MyFilesFacade.finishPicker(processId, mFragment, mNavigationInfo, FinishPickerCmd.PickerCmd.Done);
                }
                break;
            case R.id.menu_move_to_private:
                MyFilesFacade.moveToPrivate(processId, mContext, mFragment, mFragment.getSelectedFile());
                break;
            case R.id.menu_remove_from_private:
            case R.id.menu_export:
                MyFilesFacade.copymoveRecord(processId, mContext, Operation.REMOVE_FROM_PRIVATE, CopyMoveCmd.OperationProgress.SELECT_DST,
                        mFragment, mFragment.getSelectedFile(), null);
                break;
            case R.id.menu_unlock:
                MyFilesFacade.lockUnlockRecord(processId, mContext, mFragment, mFragment.getSelectedFile().get(0),
                        LockProgress.SHOW_CONFIRM_DLG);
                break;
            case R.id.menu_move_to_knox:
                MyFilesFacade.moveKnoxMode(processId, mContext, mFragment, mFragment.getSelectedFile(), KnoxMgr.TYPE_MOVE_TO_KNOX);
                break;
            case R.id.menu_move_to_enterprise_container:
                MyFilesFacade.moveKnoxMode(processId, mContext, mFragment, mFragment.getSelectedFile(), KnoxMgr.TYPE_MOVE_TO_ENTERPRISE_CONTAINER);
                break;
            case R.id.menu_move_to_secure_folder:
                MyFilesFacade.moveKnoxMode(processId, mContext, mFragment, mFragment.getSelectedFile(), KnoxMgr.TYPE_MOVE_TO_SECURE_FOLDER);
                break;
            case R.id.menu_move_to_personal_mode:
                MyFilesFacade.moveKnoxMode(processId, mContext, mFragment, mFragment.getSelectedFile(), KnoxMgr.TYPE_MOVE_TO_PERSONAL_MODE);
                break;
            case R.id.menu_remove_from_secure_folder:
                MyFilesFacade.moveKnoxMode(processId, mContext, mFragment, mFragment.getSelectedFile(), KnoxMgr.TYPE_REMOVE_FROM_SECURE_FOLDER);
                break;
            case R.id.menu_vzcloud:
                VZCloudUtils.launchVZCloud(mActivity, VZCloudUtils.VZW_CLOUD_FILES);
                break;
            case R.id.menu_change_pin:
                Intent intent = new Intent(mActivity, ChangePasswordsActivity.class);
                intent.putExtra("change_pin", true);
                try {
                    mActivity.startActivityForResult(intent, R.id.menu_change_pin);
                } catch (ActivityNotFoundException e) {
                    Log.e(this, "ActivityNotFoundException:" + e.toString());
                }
                break;
            case R.id.menu_import:
                FileRecord homePath = FileRecord.createFileRecord(StorageType.Home, "");
                NavigationManager.getInstance(processId).saveNavigationStack();
                MyFilesFacade.launchPicker(processId, homePath, NavigationMode.Pick_files, null, null, -1, null, null, null);
                break;
            case R.id.menu_toggle_list_type:
                UiUtils.toggleListType(processId, mContext, mNavigationInfo.getCurRecord());
                setMenuItemIcon(item);
                break;
            case R.id.menu_settings:
                if (curRecord != null) {
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.SETTINGS, UiUtils.isCategoryFolderList(curRecord) ?
                            SamsungAnalyticsLog.SelectMode.SELECTION_CATEGORY_FOLDER_MODE : null);
                }
                MyFilesFacade.goSetting(mFragment.getProcessId(), mFragment.getActivity());
                break;
            case R.id.menu_go_to_private:
                MyFilesFacade.goRoot(mFragment.getProcessId(), mNavigationInfo, mFragment.getActivity());
                break;
            case R.id.menu_go_to_myfiles:
                MyFilesFacade.goHome(mFragment.getProcessId(), mNavigationInfo, mFragment.getActivity());
                break;
            case R.id.menu_manage_shortcut:
                if (NavigationManager.isPossibleToActionMode()) {
                    SamsungAnalyticsLog.SelectMode selectMode = null;
                    if (mFragment.getSelectedFile() != null && !mFragment.getSelectedFile().isEmpty()) {
                        selectMode = SamsungAnalyticsLog.SelectMode.SELECTION_MODE;
                    }
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.MANAGE_SHORTCUTS, selectMode);
                    MyFilesFacade.manageShortcut(mFragment.getProcessId(), -1);
                } else {
                    Toast.makeText(mContext, R.string.another_process_is_already_in_progress, Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.menu_optimize_storage:
                if (!OptimizeStorageFileRecord.isWorking()) {
                    OptimizeStorageFileRecord.mOverviewWorking = true;
                    SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.GET_MORE_SPACE, null);
                    if (PreferenceUtils.getMoreOptionBadgeIcon(mContext)) {
                        UiUtils.sendLocalBroadcastIntent(mContext, AppConstants.MYFILES_ACTION_GMS_LAUNCHED, processId);
                        mActivity.invalidateOptionsMenu();
                    }
                    PreferenceUtils.setMoreOptionBadgeIcon(mContext, false);
                    MyFilesFacade.launchOptimizeStorage(mFragment.getProcessId(), mContext);
                } else {
                    Log.e(this, "Get More Space cannot run as multi-instance");
                }
                break;
            case R.id.menu_prepare_move_private_folder:
            case R.id.menu_prepare_remove_from_private_folder:
                Log.i(TAG, "_onOptionsItemSelected: menu_prepare_move_private_folder");
                //MyFilesFacade.moveToPrivateFolder(processId, mContext, mFragment, mFragment.getSelectedFile());
                MyFilesFacade.toSelectMode(processId, mNavigationInfo.getCurFragment(), ToSelectModeCmd.SelectModeType.EDIT_MODE);
                break;
            case R.id.menu_operator_move_private_folder:
                Log.i(TAG, "_onOptionsItemSelected: menu_operator_move_private_folder");
                MyFilesFacade.moveToPrivateFolder(processId, mContext, mFragment, mFragment.getSelectedFile());
                break;
            case R.id.menu_operator_remove_from_private_folder:
                MyFilesFacade.removeFromPrivateFolder(processId, mContext, mFragment, mFragment.getSelectedFile());
                break;
            default:
                bRet = false;
                break;
        }

        return bRet;
    }

    @Override
    public final void setActionBarNormal(ActionBar actionbar) {
        _setActionBarNormal(actionbar);
    }

    protected void _setActionBarNormal(ActionBar actionbar) {
        String actionBarTitle;
        actionBarTitle = getActionBarTitle();
        actionbar.setCustomView(null);

        actionbar.setDisplayHomeAsUpEnabled(homeAsUpEnabled());
        actionbar.setDisplayShowTitleEnabled(true);
        actionbar.setHomeButtonEnabled(homeAsUpEnabled());
        actionbar.setTitle(actionBarTitle);
    }

    protected boolean homeAsUpEnabled() {
        return true;
    }

    @Override
    public String getActionBarTitle() {
        if (AppFeatures.isTabletUIMode()) {
            return mContext.getResources().getString(R.string.app_name);
        }
        return "";
    }

    @Override
    public final boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        return _onActivityResult(requestCode, resultCode, data);
    }

    protected StorageType getTargetStorageType() {
        return StorageType.Local;
    }

    protected boolean _onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case R.id.menu_delete:
                if (mActivity instanceof FolderSelectorBottomSheetActivity) {
                    ((FolderSelectorBottomSheetActivity) mActivity).setResultAndFinish(resultCode == Activity.RESULT_OK);
                    return false;
                }
                if (resultCode == Activity.RESULT_OK) {
                    String path = null;
                    Bundle cloudBundle = null;
                    if (data != null) {
                        path = data.getStringExtra("targetPath");
                        boolean isCloud = data.getBooleanExtra(ConfirmDeleteDialog.ARGS_IS_CLOUD, false);
                        if (isCloud) {
                            cloudBundle = data.getBundleExtra(ConfirmDeleteDialog.ARGS_CLOUD_BUNDLE);
                        }
                    }

                    if (!TextUtils.isEmpty(path)) {
                        FileRecord record;
                        String categoryPath = data.getStringExtra("targetCategoryPath");
                        String categoryBucketId = data.getStringExtra("targetCategoryBucketId");
                        if (cloudBundle != null) {
                            record = new CloudFileRecord(cloudBundle.getInt(ConfirmDeleteDialog.ARGS_CLOUD_DEVICE_ID), cloudBundle.getString(ConfirmDeleteDialog.ARGS_CLOUD_PARENT_ID), cloudBundle.getString(ConfirmDeleteDialog.ARGS_CLOUD_FILE_ID), path, false);
                        } else if (categoryPath != null) {
                            record = FileRecord.createFileRecord(getTargetStorageType(), categoryPath);
                            ((CategoryFileRecord) record).setRealPath(path);
                            ((CategoryFileRecord) record).setBucketId(categoryBucketId);
                        } else {
                            record = FileRecord.createFileRecord(getTargetStorageType(), path);
                        }
                        ArrayList<FileRecord> recordList = new ArrayList<>();
                        recordList.add(record);
                        MyFilesFacade.deleteRecord(mActivity.getProcessId(), mContext, mFragment, recordList,
                                DeleteRecordCmd.OperationProgress.DO_OPERATE);
                    } else {
                        MyFilesFacade.deleteRecord(mActivity.getProcessId(), mContext, mFragment, mFragment.getSelectedFile(),
                                DeleteRecordCmd.OperationProgress.DO_OPERATE);
                    }
                }
                break;
            case R.id.menu_sort_by:
                if (resultCode == Activity.RESULT_OK) {
                    UiUtils.refreshLocalBroadcastIntent(mContext, AppConstants.MYFILES_ACTION_SORT_BY_CHANGED);
                }
                break;
            case R.id.menu_unlock:
                onLockUnlock(resultCode);
                break;
            case R.id.menu_change_pin:
                if (mFragment != null) {
                    mFragment.finishActionMode();
                }
                break;
            case FolderSelectorBottomSheetActivity.REQ_CODE_FOLDER_SELECTOR_BOTTOM_SHEET:
                if (resultCode == FolderSelectorBottomSheetActivity.RESULT_OK) {
                    FileRecord record = getCopyMoveDstRecord(data);
                    String operation = data.getStringExtra(FolderSelectorBottomSheetActivity.EXTRA_KEY_OPERATION);
                    MyFilesFacade.copymoveRecord(mFragment.getProcessId(), mContext, Operation.valueOf(operation),
                            CopyMoveCmd.OperationProgress.DO_OPERATE, mFragment, null, record);
                }
                break;
            default:
                break;
        }
        return false;
    }

    private FileRecord getCopyMoveDstRecord(Intent data) {
        String dstPath = data.getStringExtra(FolderSelectorBottomSheetActivity.EXTRA_KEY_DESTINATION_PATH);
        StorageType storageType = StorageType.valueOf(data.getStringExtra(FolderSelectorBottomSheetActivity.EXTRA_KEY_STORAGE_TYPE));
        FileRecord record = FileRecord.createFileRecord(storageType, dstPath);
        if (record != null && record.getStorageType() == StorageType.Cloud) {
            CloudFileRecord cloudFileRecord = (CloudFileRecord) record;
            cloudFileRecord.setFileId(data.getStringExtra(FolderSelectorBottomSheetActivity.EXTRA_KEY_CLOUD_FILE_ID));
            cloudFileRecord.setDeviceId(data.getIntExtra(FolderSelectorBottomSheetActivity.EXTRA_KEY_CLOUD_DEVICE_ID, 0));
            cloudFileRecord.setParentIds(data.getStringArrayListExtra(FolderSelectorBottomSheetActivity.EXTRA_KEY_CLOUD_PARENT_IDS));
            cloudFileRecord.setFileType(data.getIntExtra(FolderSelectorBottomSheetActivity.EXTRA_KEY_FILE_TYPE, AppConstants.UNDEFINED));
        }
        return record;
    }

    private void onLockUnlock(int resultCode) {
        if (mFragment == null) {
            return;
        }
        if (resultCode == Activity.RESULT_OK) {

            ArrayList<FileRecord> selectedFile = mFragment.getSelectedFile();
            if (selectedFile != null && !selectedFile.isEmpty()) {
                MyFilesFacade.lockUnlockRecord(mActivity.getProcessId(), mContext, mFragment, selectedFile.get(0),
                        LockProgress.DO_PROC);
            }
        } else {
            mFragment.finishActionMode();
        }

    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        boolean possibleActionMode = NavigationManager.isPossibleToActionMode();
        setMenuItemVisibility(menu, R.id.menu_edit, possibleActionMode && mListItemCount > 0);
        setMenuItemVisibility(menu, R.id.menu_import, false);
        setMenuItemVisibility(menu, R.id.menu_share, possibleActionMode && showShareMenu());
        setMenuItemVisibility(menu, R.id.menu_add_shortcut, false);
        setMenuItemVisibility(menu, R.id.menu_manage_shortcut, false);
        setMenuItemVisibility(menu, R.id.menu_sort_by, mListItemCount > 0);
        VZCloudUtils.prepareOptionsMenu(mFragment.getActivity(), menu, R.id.menu_vzcloud, canShowOptionsMenu());

        boolean bShowHiddenFiles = PreferenceUtils.getShowHiddenFiles(mContext);
        setMenuItemVisibility(menu, R.id.menu_show_hidden_files, !bShowHiddenFiles);
        setMenuItemVisibility(menu, R.id.menu_hide_hidden_files, bShowHiddenFiles);
        setMenuItemVisibility(menu, R.id.menu_contact_us, false);


        //debug 一直显示 GOTOPRIVATE 和 GOTOMYFILES 按钮
        setMenuItemVisibility(menu, R.id.menu_go_to_private, true);
        setMenuItemVisibility(menu, R.id.menu_go_to_myfiles, true);
        //setMenuItemVisibility(menu, R.id.menu_move_private_folder, true);

        setMenuItemVisibility(menu, R.id.menu_sync, false);
        setMenuItemVisibility(menu, R.id.menu_settings, true);
        setMenuItemVisibility(menu, R.id.menu_trash, false);
        setMenuItemVisibility(menu, R.id.menu_optimize_storage, false);
        setMenuItemVisibility(menu, R.id.menu_open, false);
        setMenuItemVisibility(menu, R.id.menu_open_in_a_new_window, false);
        setMenuItemVisibility(menu, R.id.menu_view_as, false);
        setMenuItemVisibility(menu, R.id.menu_empty, false);

        setMenuItemVisibility(menu, R.id.menu_search, !FileUtils.isEmptyStorage(mNavigationInfo, mFragment) || mFragment.getLoadingStatus());
        setMenuItemVisibility(menu, R.id.menu_toggle_list_type, mListItemCount > 0 || mFragment.getLoadingStatus());
        setMenuItemIcon(menu.findItem(R.id.menu_toggle_list_type));

        setBadgeText(menu);
    }

    protected void setMenuItemIcon(MenuItem menuItem) {
        if (menuItem != null) {
            PreferenceUtils.ViewAsKey viewAsKey = PreferenceUtils.getViewAsKey(mNavigationInfo.getCurRecord());
            int viewType = PreferenceUtils.getViewAs(mContext, viewAsKey);
            if ((viewType == AppConstants.ViewType.LIST) || viewType == AppConstants.ViewType.LIST_DETAIL) {
                menuItem.setIcon(mContext.getResources().getDrawable(R.drawable.action_bar_toggle_menu_grid_icon, mContext.getTheme()));
                menuItem.setTitle(mContext.getResources().getString(R.string.grid));
            } else {
                menuItem.setIcon(mContext.getResources().getDrawable(R.drawable.action_bar_toggle_menu_list_icon, mContext.getTheme()));
                menuItem.setTitle(mContext.getResources().getString(R.string.list));
            }
        }
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        setMenuItemVisibility(menu, R.id.menu_share, false);
        setMenuItemVisibility(menu, R.id.menu_restore, false);
        setMenuItemVisibility(menu, R.id.menu_delete, false);
        setMenuItemVisibility(menu, R.id.menu_move, false);
        setMenuItemVisibility(menu, R.id.menu_copy, false);
        setMenuItemVisibility(menu, R.id.menu_move_to_knox, false);
        setMenuItemVisibility(menu, R.id.menu_move_to_enterprise_container, false);
        setMenuItemVisibility(menu, R.id.menu_move_to_secure_folder, false);
        setMenuItemVisibility(menu, R.id.menu_move_to_personal_mode, false);
        setMenuItemVisibility(menu, R.id.menu_remove_from_secure_folder, false);
        setMenuItemVisibility(menu, R.id.menu_move_to_private, false);
        setMenuItemVisibility(menu, R.id.menu_remove_from_private, false);
        setMenuItemVisibility(menu, R.id.menu_export, false);
        setMenuItemVisibility(menu, R.id.menu_rename, false);
        setMenuItemVisibility(menu, R.id.menu_add_shortcut, false);
        setMenuItemVisibility(menu, R.id.menu_zip, false);
        setMenuItemVisibility(menu, R.id.menu_unzip, false);
        setMenuItemVisibility(menu, R.id.menu_unzip_current_folder, false);
        setMenuItemVisibility(menu, R.id.menu_unlock, false);
        setMenuItemVisibility(menu, R.id.menu_change_pin, false);
        setMenuItemVisibility(menu, R.id.menu_details, false);

        int selectedCount = mFragment.getSelectedFileCount();

        if (selectedCount > 0) {
            setMenuItemVisibility(menu, R.id.menu_copy, true);
            setMenuItemVisibility(menu, R.id.menu_move, true);
            setMenuItemVisibility(menu, R.id.menu_delete, true);
            setMenuItemVisibility(menu, R.id.menu_details, true);

            if (selectedCount == 1) {
                setMenuItemVisibility(menu, R.id.menu_rename, true);
            }
        }
    }

    protected boolean isSharableRecords(ArrayList<FileRecord> selectedItem) {
        boolean bRet = true;
        boolean totalSizeNonZero = false;
        if (selectedItem != null) {
            if ((mFragment.getActionModeType() == ActionModeType.SELECT_SHARE_FILE_OPERATION)
                    || mFragment.getActionModeType() == ActionModeType.SELECT_FILE_FOLDER_OPERATION) {
                for (FileRecord record : selectedItem) {
                    if (!UiUtils.isCheckable(mContext, record, mNavigationInfo, true)) {
                        bRet = false;
                        break;
                    }
                    totalSizeNonZero = totalSizeNonZero || isFolderLengthNonZero(SemFwWrapper.file(record.getFullPath()));
                }
            } else {
                bRet = false;
            }
        }

        return bRet && totalSizeNonZero;
    }

    @Override
    public void setMenuItemVisibility(Menu menu, int resMenuId, boolean visibility) {
        MenuItem menuItem = menu.findItem(resMenuId);

        if (menuItem != null) {
            if (visibility) {
                visibility = canShowOptionsMenu();
            }

            menuItem.setVisible(visibility);
        }
    }

    @Override
    public void setMenuItemEnabled(Menu menu, int resMenuId, boolean enable) {
        MenuItem menuItem = menu.findItem(resMenuId);

        if (menuItem != null) {
            menuItem.setEnabled(enable);
        }
    }

    protected static void showMoreMenu(Menu menu, int resMenuId, boolean visibility) {
        MenuItem menuItem = menu.findItem(resMenuId);

        if (menuItem != null) {
            menuItem.setShowAsAction(visibility ? MenuItem.SHOW_AS_ACTION_NEVER : MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
    }

    protected boolean showShareMenu() {
        boolean show = false;
        FileRecord record;
        for (int i = 0; i < mFragment.getTotalFileCount(); i++) {
            record = mFragment.getFileRecord(i);
            if (record != null && UiUtils.isShareAvailable(mContext, record) && FileUtils.isSameAsRealFile(record)) {
                if (DrmUtils.isForwardable(mContext, record.getFullPath())) {
                    show = true;
                    break;
                }
            }
        }
        return show;
    }

    protected boolean showShareMenu(FileRecord record) {
        return (record != null && UiUtils.isShareAvailable(mContext, record)
                && FileUtils.isSameAsRealFile(record) && DrmUtils.isForwardable(mContext, record.getFullPath()));
    }

    protected PrivatePath getSelectedItemPrivatePathStatus(ArrayList<FileRecord> selectedItem) {
        boolean isOnlyPrivate = true;
        boolean isOnlyNonPrivate = true;

        PrivateModeMgr privateModeMgr = PrivateModeMgr.getInstance(mContext);
        for (FileRecord record : selectedItem) {
            if (record != null && privateModeMgr.isPrivateModePath(record.getFullPath())) {
                isOnlyNonPrivate = false;
            } else {
                isOnlyPrivate = false;
            }
        }

        if (!isOnlyNonPrivate && !isOnlyPrivate)
            return PrivatePath.BOTH;
        else if (isOnlyNonPrivate)
            return PrivatePath.ONLY_NON_PRIVATE;
        else
            return PrivatePath.ONLY_PRIVATE;
    }

    @Override
    public String getSelectedItemString(int selectedCnt) {
        String ret;
        if (selectedCnt <= 0) {
            ret = mContext.getString(R.string.select_file);
        } else {
            if (AppFeatures.isTablet()) {
                ret = mContext.getResources().getString(R.string.n_selected, selectedCnt);

            } else {
                ret = String.format(Locale.getDefault(), "%d", selectedCnt);
            }
        }
        return ret;
    }

    protected boolean canShowOptionsMenu() {
        boolean bRet = true;
        if (mFragment != null && mFragment.getActionModeType() == ActionModeType.NORMAL) {
            if (mActivity.isSearchViewExpanded() && !AppFeatures.isTabletUIMode()) {
                bRet = false;
            }
        }
        return bRet;
    }

    protected boolean isMyFilesHome() {
        boolean bRet = false;
        NavigationManager nm = NavigationManager.getInstance(mFragment.getProcessId());

        if (nm != null && nm.getDepth() == 1) {
            bRet = true;
        }

        return bRet;
    }

    protected void showKnoxMenu(Menu menu, boolean isDisableKnox, PrivatePath pathSts) {
        KnoxMgr knoxMgr = KnoxMgr.getInstance(mContext);
        if (knoxMgr.isKnoxNameChangeable()) {
            if (AppFeatures.isKnox()) {
                if (knoxMgr.isSecureFolder()) {
                    setKnoxMenuItemVisibility(KnoxMgr.TYPE_REMOVE_FROM_SECURE_FOLDER, menu, R.id.menu_remove_from_secure_folder, R.string.menu_move_out_of_secure_folder_ps, true);
                } else {
                    setMenuItemVisibility(menu, R.id.menu_move_to_personal_mode, true);
                }
            } else {
                setKnoxMenuItemVisibility(KnoxMgr.TYPE_MOVE_TO_SECURE_FOLDER, menu, R.id.menu_move_to_secure_folder, R.string.menu_move_to_secure_folder_ps, !isDisableKnox);
                setKnoxMenuItemVisibility(KnoxMgr.TYPE_MOVE_TO_KNOX, menu, R.id.menu_move_to_knox, R.string.menu_move_to_ps, !isDisableKnox);
                setKnoxMenuItemVisibility(KnoxMgr.TYPE_MOVE_TO_ENTERPRISE_CONTAINER, menu, R.id.menu_move_to_enterprise_container, R.string.menu_move_to_ps, !isDisableKnox);
            }
        } else {
            if (knoxMgr.isSupportMove() && pathSts == PrivatePath.ONLY_NON_PRIVATE) {
                if (!AppFeatures.isKnox()) {
                    setMenuItemVisibility(menu, R.id.menu_move_to_knox, !isDisableKnox);
                } else {
                    setMenuItemVisibility(menu, R.id.menu_move_to_personal_mode, true);
                }
            }
        }
    }

    private void setKnoxMenuItemVisibility(int type, Menu menu, int menuId, int stringId, boolean visibility) {
        String knoxName = KnoxMgr.getInstance(mContext).getKnoxName(type);
        if (!TextUtils.isEmpty(knoxName)) {
            MenuItem menuItem = menu.findItem(menuId);
            if (menuItem != null) {
                menuItem.setTitle(mContext.getString(stringId, knoxName));
                setMenuItemVisibility(menu, menuId, visibility);
            }
        }
    }

    private void addKnoxMenuItem(int type, Menu menu, int menuId, int stringId) {
        String knoxName = KnoxMgr.getInstance(mContext).getKnoxName(type);
        if (!TextUtils.isEmpty(knoxName)) {
            menu.add(0, menuId, 0, mContext.getString(stringId, knoxName));
        }
    }

    @Override
    public void onPause() {
    }

    private void createContextualMenuOfOpen(ContextMenu menu, boolean isDir) {
        menu.add(0, R.id.menu_open, 0, mContext.getResources().getString(R.string.menu_open));
        if (isDir) {
            menu.add(0, R.id.menu_open_in_a_new_window, 0, mContext.getResources().getString(R.string.menu_open_in_a_new_window));
        }
    }

    private void createContextualMenuOnItems(ContextMenu menu, AbsMyFilesFragment fragment, FileRecord record, boolean isNormalMode) {
        if (isNormalMode) {
            createContextualMenuOfOpen(menu, record.isDirectory());
        }
        int selectedCount = fragment.getSelectedFileCount();
        if (selectedCount > 0 || isNormalMode) {
            boolean isForOneItem = (isNormalMode) || (selectedCount == 1);
            _createContextualMenuOnItems(menu, record, isForOneItem);
        }
    }

    protected void _createContextualMenuOnItems(ContextMenu menu, FileRecord record, boolean isForOneItem) {
        if (MouseKeyboardMgr.isCtrlCopied() || MouseKeyboardMgr.isCtrlMoved()) {
            if (record.isDirectory()) {
                menu.add(0, R.id.menu_paste, 0, mContext.getResources().getString(R.string.menu_paste));
            } else {
                menu.add(0, R.id.menu_move, 0, mContext.getResources().getString(R.string.menu_move));
                menu.add(0, R.id.menu_copy, 0, mContext.getResources().getString(R.string.menu_copy));
            }
        } else {
            menu.add(0, R.id.menu_delete, 0, mContext.getResources().getString(R.string.menu_delete));
            if (NavigationManager.isPossibleToActionMode() && showShareMenu(record)) {
                menu.add(0, R.id.menu_share, 0, mContext.getResources().getString(R.string.menu_share));
            }
            menu.add(0, R.id.menu_move, 0, mContext.getResources().getString(R.string.menu_move));
            menu.add(0, R.id.menu_copy, 0, mContext.getResources().getString(R.string.menu_copy));
        }
        if (isForOneItem) {
            menu.add(0, R.id.menu_rename, 0, mContext.getResources().getString(R.string.menu_rename));
            if (ShortcutUtils.isSupportShortcut(mContext, record)) {
                menu.add(0, R.id.menu_add_shortcut, 0, mContext.getResources().getString(R.string.add_shortcut));
            }
        }
        if (record.getFileType() != FileType.ZIP) {
            menu.add(0, R.id.menu_zip, 0, mContext.getResources().getString(R.string.menu_zip));
        } else {
            menu.add(0, R.id.menu_unzip, 0, mContext.getResources().getString(R.string.menu_unzip));
            menu.add(0, R.id.menu_unzip_current_folder, 0, mContext.getResources().getString(R.string.menu_unzip_current_folder));
        }

        PrivateModeMgr privateModeMgr = PrivateModeMgr.getInstance(mContext);
        if (privateModeMgr.isReady()) {
            if (privateModeMgr.isPrivateModePath(record.getFullPath())) {
                menu.add(0, R.id.menu_remove_from_private, 0, mContext.getResources().getString(R.string.remove_from_private));
            } else {
                menu.add(0, R.id.menu_move_to_private, 0, mContext.getResources().getString(R.string.move_to_private));
            }
        }
        showKnoxMenuOnContextualMenu(menu);
        menu.add(0, R.id.menu_details, 0, mContext.getResources().getString(R.string.menu_details));
    }

    private void showKnoxMenuOnContextualMenu(ContextMenu menu) {
        KnoxMgr knoxMgr = KnoxMgr.getInstance(mContext);
        if (knoxMgr.isKnoxNameChangeable()) {
            if (AppFeatures.isKnox()) {
                if (knoxMgr.isSecureFolder()) {
                    addKnoxMenuItem(KnoxMgr.TYPE_REMOVE_FROM_SECURE_FOLDER, menu, R.id.menu_remove_from_secure_folder, R.string.menu_move_out_of_secure_folder_ps);
                } else {
                    menu.add(0, R.id.menu_move_to_personal_mode, 0, R.string.menu_move_to_personal_mode);
                }
            } else {
                addKnoxMenuItem(KnoxMgr.TYPE_MOVE_TO_SECURE_FOLDER, menu, R.id.menu_move_to_secure_folder, R.string.menu_move_to_secure_folder_ps);
                addKnoxMenuItem(KnoxMgr.TYPE_MOVE_TO_KNOX, menu, R.id.menu_move_to_knox, R.string.menu_move_to_ps);
                addKnoxMenuItem(KnoxMgr.TYPE_MOVE_TO_ENTERPRISE_CONTAINER, menu, R.id.menu_move_to_enterprise_container, R.string.menu_move_to_ps);
            }
        }
    }

    protected void createContextualMenuOnBackground(ContextMenu menu) {
        boolean existItems = mFragment.getListItemCount() > 0;
        menu.add(0, R.id.menu_create_folder, 1, mContext.getResources().getString(R.string.menu_create_folder));

        //TODO 这是做什么的？
        //menu.add(0, R.id.menu_move_private_folder, 0, "Test Move private folder");

        if (existItems) {
            if (NavigationManager.isPossibleToActionMode()) {
                menu.add(0, R.id.menu_edit, 0, mContext.getResources().getString(R.string.menu_edit));
            }
            menu.add(0, R.id.menu_share, 2, mContext.getResources().getString(R.string.menu_share));
            menu.add(0, R.id.menu_sort_by, 4, mContext.getResources().getString(R.string.menu_sort_by));
        }
        if (MouseKeyboardMgr.isCtrlCopied() || MouseKeyboardMgr.isCtrlMoved()) {
            menu.add(0, R.id.menu_paste, 3, mContext.getResources().getString(R.string.menu_paste));
        }
        if (!PreferenceUtils.getShowHiddenFiles(mContext)) {
            menu.add(0, R.id.menu_show_hidden_files, 5, mContext.getString(R.string.menu_show_hidden_files));
        } else {
            menu.add(0, R.id.menu_hide_hidden_files, 5, mContext.getString(R.string.menu_hide_hidden_files));
        }
    }

    @Override
    public final boolean onContextualItemSelected(MenuItem item, AbsMyFilesFragment fragment, FileRecord record) {
        return _onContextualItemSelected(item, null, record, fragment);
    }

    protected boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        boolean bRet = true;

        int processId = fragment.getProcessId();
        switch (item.getItemId()) {
            case R.id.menu_open:
                MyFilesFacade.executeRecord(processId, fragment.getFileListActivity(), record);
                break;
            case R.id.menu_open_in_a_new_window:
                String path = record.getFullPath();
                Intent intent = new Intent(AppConstants.Action.ACTION_LAUNCH_MY_FILES);
                intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(AppConstants.MyFilesExtras.EXTRA_START_PATH, path);
                mContext.startActivity(intent);
                break;
            case R.id.menu_delete:
                DeleteRecordCmd.OperationProgress progress = DeleteRecordCmd.OperationProgress.SHOW_SINGLE_CONFIRM_POPUP;
                if (fragment.isSelectActionMode()) {
                    progress = DeleteRecordCmd.OperationProgress.SHOW_CONFIRM_POPUP;
                }
                MyFilesFacade.deleteRecord(processId, mContext, fragment, srcRecordList, progress);
                break;
            case R.id.menu_share:
                MyFilesFacade.shareVia(processId, mContext, fragment, srcRecordList, false);
                break;
            case R.id.menu_move:
                MyFilesFacade.copymoveRecord(processId, mContext, FileOperator.Operation.MOVE, CopyMoveCmd.OperationProgress.SELECT_DST, fragment,
                        srcRecordList, null);
                break;
            case R.id.menu_copy:
                MyFilesFacade.copymoveRecord(processId, mContext, FileOperator.Operation.COPY, CopyMoveCmd.OperationProgress.SELECT_DST, fragment,
                        srcRecordList, null);
                break;
            case R.id.menu_paste:
                if (AppFeatures.isKnoxDesktopMode()) {
                    FileRecord curRecord = null;
                    if (srcRecordList != null && srcRecordList.size() > 0) {
                        curRecord = srcRecordList.get(0);
                    } else {
                        NavigationInfo info = mFragment.getNavigationInfo();
                        if (info != null) {
                            curRecord = info.getCurRecord();
                        }
                    }
                    if (curRecord != null) {
                        MouseKeyboardMgr.onCtrlV(mFragment, curRecord);
                    } else {
                        Log.e(this, "record is null");
                    }
                }
                break;
            case R.id.menu_rename:
                MyFilesFacade.renameRecord(processId, mContext, fragment, srcRecordList);
                break;
            case R.id.menu_zip:
                MyFilesFacade.compress(processId, mContext, fragment, srcRecordList);
                break;
            case R.id.menu_unzip:
                MyFilesFacade.decompress(processId, mContext, fragment, srcRecordList);
                break;
            case R.id.menu_unzip_current_folder:
                MyFilesFacade.decompressToCurrentFolder(processId, mContext, fragment, srcRecordList);
                break;
            case R.id.menu_move_to_private:
                MyFilesFacade.moveToPrivate(processId, mContext, fragment, srcRecordList);
                break;
            case R.id.menu_details:
                MyFilesFacade.showDetail(processId, mContext, fragment, srcRecordList, false);
                break;
            case R.id.menu_edit:
                MyFilesFacade.toSelectMode(processId, fragment, ToSelectModeCmd.SelectModeType.EDIT_MODE);
                break;
            case R.id.menu_sort_by:
                MyFilesFacade.setSortBy(processId, fragment);
                break;
            case R.id.menu_show_hidden_files:
            case R.id.menu_hide_hidden_files:
                MyFilesFacade.setShowHiddenFiles(processId, mContext, fragment.getNavigationInfo().getCurFragment());
                UiUtils.sendLocalBroadcastIntent(mContext, AppConstants.MYFILES_ACTION_SHOW_HIDDEN_FILES_CHANGED, processId);
                break;
            case R.id.menu_remove:
                MyFilesFacade.deleteRecord(processId, mContext, fragment, srcRecordList, DeleteRecordCmd.OperationProgress.DO_OPERATE);
                break;
            case R.id.menu_clear_recent_history:
                MyFilesFacade.clearRecent(processId, mContext);
                break;
            case R.id.menu_create_folder:
                MyFilesFacade.createDir(processId, mContext, fragment, record);
                break;
            case R.id.menu_clear_history:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.CLEAR_HISTORY, null);
                MyFilesFacade.clearDownloadHistory(processId, fragment);
                break;
            case R.id.menu_add_shortcut:
                MyFilesFacade.addShortcut(processId, mContext, fragment, srcRecordList);
                break;
            case R.id.menu_manage_shortcut:
                MyFilesFacade.manageShortcut(processId, -1);
                break;
            case R.id.menu_remove_shortcut:
                MyFilesFacade.deleteRecord(processId, mContext, fragment, srcRecordList,
                        DeleteRecordCmd.OperationProgress.DO_OPERATE);
                break;
            case R.id.menu_move_to_knox:
                MyFilesFacade.moveKnoxMode(processId, mContext, fragment, srcRecordList, KnoxMgr.TYPE_MOVE_TO_KNOX);
                break;
            case R.id.menu_move_to_enterprise_container:
                MyFilesFacade.moveKnoxMode(processId, mContext, fragment, srcRecordList, KnoxMgr.TYPE_MOVE_TO_ENTERPRISE_CONTAINER);
                break;
            case R.id.menu_move_to_secure_folder:
                MyFilesFacade.moveKnoxMode(processId, mContext, fragment, srcRecordList, KnoxMgr.TYPE_MOVE_TO_SECURE_FOLDER);
                break;
            case R.id.menu_move_to_personal_mode:
                MyFilesFacade.moveKnoxMode(processId, mContext, fragment, srcRecordList, KnoxMgr.TYPE_MOVE_TO_PERSONAL_MODE);
                break;
            case R.id.menu_remove_from_secure_folder:
                MyFilesFacade.moveKnoxMode(processId, mContext, fragment, srcRecordList, KnoxMgr.TYPE_REMOVE_FROM_SECURE_FOLDER);
                break;
            default:
                bRet = false;
                break;
        }

        return bRet;
    }

    public void onCreateContextualMenu(ContextMenu menu, AbsMyFilesFragment fragment, FileRecord record) {
        createContextualMenu(menu, fragment, record);
    }

    public void createContextualMenu(ContextMenu menu, AbsMyFilesFragment fragment, FileRecord record) {
        if (fragment != null) {
            NavigationInfo info = fragment.getNavigationInfo();
            if (info != null && !info.isSelectOpDestination()) {
                AbsMyFilesFragment.ActionModeType actionModeType = fragment.getActionModeType();
                boolean isNormalMode = (actionModeType == AbsMyFilesFragment.ActionModeType.NORMAL);
                if (fragment.getTouchPosition() == BACKGROUND) {
                    if (actionModeType == AbsMyFilesFragment.ActionModeType.NORMAL) {
                        createContextualMenuOnBackground(menu);
                    }
                } else if (actionModeType != ActionModeType.SELECT_SHARE_FILE_OPERATION) { // on Items
                    if (fragment.getPanelInfo() == AppConstants.PanelInfo.LeftPanel) {
                        createContextualMenuOnLeftPanel(menu, record);
                    } else {
                        createContextualMenuOnItems(menu, fragment, record, isNormalMode);
                    }
                }
            }
        }
    }

    protected void createContextualMenuOnLeftPanel(ContextMenu menu, FileRecord record) {
    }

    @Override
    public final void onUpdateSelectLayoutMenu(Menu menu, NavigationInfo navigationInfo) {
        NavigationMode navigationMode = navigationInfo.getNavigationMode();
        if (navigationMode == NavigationMode.Pick_files) {
            boolean selectedFileExist = mFragment.getSelectedFileCount() > 0;
            setMenuItemVisibility(menu, R.id.menu_pick_done, selectedFileExist);
        } else {
            _onUpdateSelectLayoutMenu(menu, navigationMode);
        }
    }

    protected void _onUpdateSelectLayoutMenu(Menu menu, NavigationMode navigationMode) {
    }

    public void setBadgeText(Menu menu) {
        if (SmartTipUtils.canShowSmartTip(mContext)) {
            // If Gms Smart tip is seen or is planning to draw, don't draw a badge icon.
            if (SmartTipUtils.planToDrawGmsSmartTip(mContext)) {
                return;
            }
        }
        MenuItem item;
        if (PreferenceUtils.getMoreOptionBadgeIcon(mContext)) {
            item = (MenuItem) menu.findItem(R.id.menu_optimize_storage);
            if (item != null) {
                //TODO HYL
                //item.setBadgeText(mContext.getResources().getString(R.string.new_badge));
            }
        }

        boolean curBadgeForUpdate = PreferenceUtils.getAvailAppUpdate(mContext);
        if (curBadgeForUpdate) {
            item = (MenuItem) menu.findItem(R.id.menu_settings);
            if (item != null) {
                //item.setBadgeText("1");
            }
        }
    }

    private boolean isFolderLengthNonZero(File file) {
        if (file == null || !file.exists()) {
            return false;
        }

        ArrayList<File> arrayList = new ArrayList<>();
        arrayList.add(file);
        for (int count = 0; count < arrayList.size(); ++count) {
            File temp = arrayList.get(count);
            if (temp.isDirectory()) {
                File[] fileList = temp.listFiles();
                if (fileList != null) {
                    for (File content : fileList) {
                        arrayList.add(content);
                    }
                }
            } else {
                if (temp.length() > 0) {
                    return true;
                }
            }
        }
        return false;
    }

}
